package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.common.enums.ErrorCode;
import com.starlink.common.exception.BusinessException;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.StarHealthDtoMapper;
import com.starlink.model.domain.StarHealthDO;
import com.starlink.mapper.StarHealthMapper;
import com.starlink.model.request.StarHealthRequest;
import com.starlink.service.StarHealthService;
import com.starlink.model.util.StarHealthConvert;
import com.starlink.model.vo.StarHealthVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;
import java.util.Objects;

/**
 * @author zhanghong
 */
@Service
public class StarHealthServiceImpl extends ServiceImpl<StarHealthMapper, StarHealthDO> implements StarHealthService {
    @Autowired
    private StarHealthMapper starHealthMapper;

    @Override
    public StarHealthVO getByKey(long id) {
        StarHealthVO starHealthVO = StarHealthDtoMapper.INSTANCE.do2VO(getById(id));
        return starHealthVO;
    }

    @Override
    public PageInfo<StarHealthVO> pageQuery(Pageable pageable,StarHealthRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<StarHealthDO> list = starHealthMapper.listQuery(request);
        List<StarHealthVO> starHealthVOList = StarHealthConvert.buildStarHealthVoList(list);
        return new PageInfo(starHealthVOList);
    }

    @Override
    public List<StarHealthVO> listQuery(StarHealthRequest request) {
        List<StarHealthDO> list = starHealthMapper.selectDOList(StarHealthDtoMapper.INSTANCE.request2DO(request));
        List<StarHealthVO> starHealthVOS = StarHealthConvert.buildStarHealthVoList(list);
        return starHealthVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(StarHealthRequest request) {
        boolean exists = lambdaQuery().eq(StarHealthDO::getType, request.getType()).eq(StarHealthDO::getSortNum, request.getSortNum()).exists();
        AssertUtil.assertFalse(exists, "该类型下排序不能重复");
        try {
            save(StarHealthDtoMapper.INSTANCE.request2DO(request));
        } catch (Exception ex) {
            if (ex.getMessage().contains("Data too long")) {
                throw new BusinessException(ErrorCode.PARAM_ERROR, "详情内容超长，请优化！");
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(StarHealthRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        StarHealthDO one = lambdaQuery().eq(StarHealthDO::getType, request.getType()).eq(StarHealthDO::getSortNum, request.getSortNum()).last("limit 1").one();
        if (Objects.nonNull(one)) {
            AssertUtil.assertTrue(Objects.equals(one.getId(), request.getId()), "该类型下排序不能重复");
        }
        try {
            saveOrUpdate(StarHealthDtoMapper.INSTANCE.request2DO(request));
        } catch (Exception ex) {
            if (ex.getMessage().contains("Data too long")) {
                throw new BusinessException(ErrorCode.PARAM_ERROR, "详情内容超长，请优化！");
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        starHealthMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
